package com.yunkaweilai.android.fragment.member;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;

import com.github.lazylibrary.util.StringUtils;
import com.google.gson.Gson;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.activity.login.LoginActivity;
import com.yunkaweilai.android.base.BaseApplication;
import com.yunkaweilai.android.base.BaseFragment;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.model.WebFragmentModel;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.Utils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class WebFragment extends BaseFragment {
    @BindView(R.id.id_webView)
    WebView idWebView;
    @BindView(R.id.img_nothing)
    ImageView imageViewNothing;
    Unbinder unbinder;
    private String Url = "";
    private WebSettings mWebSettings;


    public static WebFragment newInstance(String URL) {
        WebFragment fragment = new WebFragment();
//        fragment.Url = URL;
        return fragment;
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_web_full;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (StringUtils.isEmpty(Url)) {
            Url = "http://m.hfwjx.com";
        }
        initHttp();


    }

    private void initHttp() {
        HttpRequest.postUrl(Api.APP_CHECK_WECHAT_CENTER)
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        Gson gson = new Gson();
                        boolean checkcode = Utils.checkCode(mAc,json);
                        if (checkcode) {
                            WebFragmentModel webFragmentModel = new WebFragmentModel();
                            webFragmentModel = gson.fromJson(json, WebFragmentModel.class);
                            Url = webFragmentModel.getData().getInfo().getUrl();
                            if (StringUtils.isEmpty(Url)) {
                                Url = "http://m.hfwjx.com";
                            } else {
                                    Url = Url + "?token=" + BaseApplication.getToken();
                            }
                            openweb();
                        }
                    }

                    @Override
                    public void onError(Exception e) {

                    }
                });
    }

    private void openweb() {
        mWebSettings = idWebView.getSettings();
        mWebSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        mWebSettings.setJavaScriptEnabled(true);
        idWebView.addJavascriptInterface(new JSInterface(), "Order");
        idWebView.loadUrl(Url);

        //设置WebChromeClient类
        idWebView.setWebChromeClient(new WebChromeClient() {
            //获取网站标题
            @Override
            public void onReceivedTitle(WebView view, String title) {
//                titlebarTv.setText(title);
//                titlebarTv.setVisibility(View.VISIBLE);
            }

            //获取加载进度
            @Override
            public void onProgressChanged(WebView view, int newProgress) {

            }
        });

        //设置不用系统浏览器打开,直接显示在当前Webview
        idWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });

        //设置WebViewClient类
        idWebView.setWebViewClient(new WebViewClient() {
            //设置加载前的函数
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
            }

            //设置结束加载函数
            @Override
            public void onPageFinished(WebView view, String url) {
            }
        });
    }


    //销毁Webview
    @Override
    public void onDetach() {
        if (idWebView != null) {
            idWebView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
            idWebView.clearHistory();
            ((ViewGroup) idWebView.getParent()).removeView(idWebView);
            idWebView.destroy();
            idWebView = null;
        }
        super.onDetach();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private class JSInterface {
        @JavascriptInterface
        public void OrderInfo(String tid) {//复制订单号
            if (!StringUtils.isEmpty(tid)) {
                Utils.copy2clipboard(mAc, tid);
                showToast("复制成功");
            }
        }

        @JavascriptInterface
        public void OrderRefund() {//退款成功
            showToast("退款成功");
        }

        @JavascriptInterface
        public void GotoLogin() {//退款成功
            showToast("登录失效");
            intent2Activity(LoginActivity.class);
        }
    }
}
